<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8" %>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://"
            + request.getServerName() + ":" + request.getServerPort()
            + path + "/";
%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>五指山污水处理系统</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="<%=request.getContextPath()%>/huazhi/lib/layui-v2.5.5/css/layui.css" media="all">
    <link rel="stylesheet" href="<%=request.getContextPath()%>/huazhi/css/public.css" media="all">
    <link rel="shortcut icon" href="<%=request.getContextPath()%>/huazhi/images/favicon.ico"/>
    <link rel="stylesheet" href="<%=request.getContextPath()%>/css/logincss/layout.css" media="all">
</head>
<style>


</style>
<body>
<div class="layui-fluid">

        <div >
            <form class="layui-form"  style="margin-top: 30%;">

                <div class="layui-form-item">
                    <label class="layui-form-label">乡镇</label>
                    <div class="layui-input-block">
                        <select  id="xz-select" name="xz" lay-filter="xz-select"
                                 class="layui-input">
                        </select>
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label">村委会</label>
                    <div class="layui-input-block">
                        <select  name="cwh" lay-filter="cwh-select" id="cwh-select"
                                 class="layui-input">
                        </select>
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label">自然村</label>
                    <div class="layui-input-block">
                        <select  name="zrc" lay-filter="zrc-select" id="zrc-select"
                                 class="layui-input">
                        </select>
                    </div>
                </div>

            </form>
            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button type="button" class="layui-btn layui-btn-normal" id="whxc" >维护查询</button>
                    <button type="button" class="layui-btn layui-btn-normal" id="xzwh">新增维护</button>
                </div>
            </div>
<%--            <div class="layui-row">--%>
<%--               <div class="layui-col-lg6" style="margin: 0 auto;">--%>
<%--                  <div>--%>
<%--                     <button type="button" class="layui-btn layui-btn-normal">维护查询</button>--%>
<%--                      <button type="button" class="layui-btn layui-btn-normal">新增维护</button>--%>
<%--                  </div>--%>
<%--               </div>--%>
<%--            </div>--%>

        </div>
    </div>
</div>
<script src="<%=request.getContextPath()%>/huazhi/lib/layui-v2.5.5/layui.all.js"></script>
<script src="<%=request.getContextPath()%>/huazhi/js/lay-config.js?v=1.0.4" charset="utf-8"></script>
<script>
    layui.use(['form', 'laydate', 'layer'], function () {
        var $ = layui.$
            , form = layui.form
            , laydate = layui.laydate
            , layer = layui.layer



        //自定义验证规则
        form.verify({
        });

        $.ajax({
            type: "get",
            url: "<%=request.getContextPath()%>/rest/base/getXzglSelect",
            dataType: "json",
            async: false,
            data:{
                xzzdId:-1
            },
            success: function (msg) {
                document.getElementById("xz-select").options.length = 0;
                var content = '<option value="">请选择乡镇</option>';
                var  xzSelect = $("#xz-select");
                for (var i = 0;i<msg.xzzdList.length;i++){
                    content+='<option value="' + msg.xzzdList[i].id + '">' + msg.xzzdList[i].title + '</option>';;
                }
                xzSelect.append(content)
                form.render()
            }

        })


        //监听乡镇select
        form.on('select(xz-select)',function (data){
            //给村委会赋值
            var cwhselect = $("#cwh-select");
            $.ajax({
                type: "post",
                url: "<%=request.getContextPath()%>/rest/base/getXzglSelect",
                async: false,
                data:{
                    'xzzdId':data.value,
                },
                success: function (msg) {
                    document.getElementById("cwh-select").options.length = 0;
                    var content = '<option value="">请选择村委会</option>';
                    var  cwhSelect = $("#cwh-select");
                    for (var i = 0;i<msg.xzzdList.length;i++){
                        content+='<option value="' + msg.xzzdList[i].id + '">' + msg.xzzdList[i].title + '</option>';;
                    }
                    cwhSelect.append(content)
                    form.render();
                }
            })

        })

        //监听村委会select
        form.on('select(cwh-select)',function (data){
            //给自然村赋值
            var cwhselect = $("#zrc-select");
            $.ajax({
                type: "post",
                url: "<%=request.getContextPath()%>/rest/base/getZrcs",
                dataType: "json",
                async: false,
                data:{
                    'xzzdId':data.value,
                },
                success: function (msg) {
                    var  content = ""
                    document.getElementById("zrc-select").options.length = 0;
                    if(msg.xzzdList.length>0){
                        content = '<option value="">请选择自然村</option>';
                    }else {
                        content = '<option value="">没有可选择的自然村</option>';
                    }
                    var  zrcSelect = $("#zrc-select");
                    for (var i = 0;i<msg.xzzdList.length;i++){
                        content+='<option value="' + msg.xzzdList[i].id + '">' + msg.xzzdList[i].title + '</option>';;
                    }
                    zrcSelect.append(content)
                    form.render();
                }
            })

        })

        function  getSelectvalue(){
         var xzId = document.getElementById("xz-select").value;
         var cwhId = document.getElementById("cwh-select").value;
         var zrcId = document.getElementById("zrc-select").value;
         var xzSelect = {
             xzId :xzId,
             cwhId:cwhId,
             zrcId:zrcId
         }
         return xzSelect
        }

        $(document).on('click',"#xzwh",function(){
            console.log("新增维护")
            $.ajax({
                type: "post",
                url: "<%=request.getContextPath()%>/rest/base/xzwh",
                data:getSelectvalue(),
                async: false,
                success :function (flag){
                    if (flag){
                        window.location.href="<%=request.getContextPath()%>/phone/xzwh.jsp"
                    }
                }
            })
        });

        $(document).on('click',"#whxc",function(){
            var obj = getSelectvalue();
            if(obj.zrcId == ''||obj.zrcId == null){
                layer.msg("请选择自然村")
            }else {
                var data = {xcZrcId : obj.zrcId}
                $.ajax({
                    type: "post",
                    url: "<%=request.getContextPath()%>/rest/base/whxc",
                    data:data,
                    async: false,
                    success :function (flag){
                        if (flag){
                            window.location.href="<%=request.getContextPath()%>/phone/phoneRili.jsp"
                        }

                    }
                })
            }

        });

    });
</script>
</body>
</html>